<?php


/**
 * Notes:
 * PROJECT_NAME : ERP v1.8 重构erp
 * User: 夏鱼儿<397072174@qq.com>
 * Date: 2024/3/13
 * Time: 17:22
 */
namespace app\admin\controller;

use app\admin\model\Goods as GoodsModel;
use app\admin\model\GoodsRule;
use app\admin\traits\Crud;
use app\admin\validate\GoodsValidate;
use Exception;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\facade\Request;
use think\Response;

class Goods extends BaseAdminController
{
    private GoodsModel $model;
    use Crud;

    public function initialize()
    {
        parent::initialize();
        $this->model             = new GoodsModel();
        $this->validateClass     = GoodsValidate::class;
        $this->searchFields      = "goods_id,status,id";
        $this->detailWithLink    = "goods_rule,spec";
    }


    /**
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function list(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('list')->check($param);
        $sql = new GoodsRule();
        $sql = $sql->withSearch($this->searchFields, $param);
        $count = $sql->count();
        if (Request::param('is_export', 0) != 1) $sql = $sql->page($param['page'], $param['limit']);
        $list = $sql->order($this->orderSort)->select()->each(function ($item){
            $item->goods = $item->goods()->find();
        });
        return $this->success('获取成功', compact('count', 'list'));
    }


    /**
     * 编辑状态
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function editStatus(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('editStatus')->check(Request::param());
        $this->model->startTrans();
        try {
            $sql = new GoodsRule();
            if (!$info = $sql->where('id', $param['id'])->find()) exception('数据不存在');
            unset($param['id']);
            if (empty($param)) exception('请填写要修改的内容');
            $info->save($param);
            $this->model->commit();
        } catch (Exception $e) {
            $this->model->rollback();
            return $this->error($e->getMessage());
        }
        return $this->success('修改成功');
    }


    /**
     * 真删除
     * @return Response
     */
    public function del(): Response
    {
        $param = Request::param();
        validate($this->validateClass)->scene('del')->check(Request::param());
        $this->model->startTrans();
        try {
            GoodsRule::whereIN('id', explode(',', $param['id']))->select()->each(function ($item) {
                $item->delete();
            });
            $this->model->commit();
        } catch (Exception $e) {
            $this->model->rollback();
            return $this->error($e->getMessage());
        }
        return $this->success('删除成功');
    }
}